<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryObj.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="hInput"
        />
        <el-tree
          ref="tree"
          :data="depts"
          :props="defaultProps"
          :default-expanded-keys="[1]"
          :expand-on-click-node="false"
          highlight-current
          node-key="id"
          check-strictly
          @current-change="hCurrentChange"
        >
          <!-- show-checkbox -->
        <!-- <template slot-scope="{ node, data }" /> -->
        </el-tree>
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="space-between">
          <el-row style="height: 30px" align="middle" type="flex" justify="end">
            <el-pagination
              layout="total,prev, pager, next"
              :total="total"
              background
              @current-change="handleCurrent"
            />
          </el-row>
          <el-row>
            <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
            <el-button v-allow="'excel_export'" size="mini" @click="isShowExcel = true">excel导入</el-button>
            <el-button v-allow="'excel_inport'" size="mini" @click="hExcel">excel导出</el-button>
            <!-- v-if="$store.state.user.userInfo.roles.points.includes('excel_export')" -->
          </el-row>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employList">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template slot-scope="{row}">
              <img v-if="row.staffPhoto" :src="row.staffPhoto" style="width:30px">
              <span v-else class="username">{{ row.username.charAt(1) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template slot-scope="{row}">
              <!-- {{ row.formOfEmployment }} -->
              <span v-if="row.formOfEmployment === 1"> 正式员工 </span>
              <span v-else-if="row.formOfEmployment === 2"> 试用员工 </span>
              <span v-else> 临时员工 </span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template #default="{row}">
              <el-button size="mini" type="text" @click="$router.push('/employee/detail/' + row.id )">查看</el-button>
              <el-button size="mini" type="text" @click="assignShow(row.id)" class="buttonsss">角色</el-button>
              <el-popconfirm
                confirm-button-text='是的'
                cancel-button-text='不用了'
                icon="el-icon-info"
                icon-color="red"
                title="确定要删除该员工吗？"
                @onConfirm="delUser(row.id)"
              >
                <!-- @cancel="" -->
                <el-button size="mini" slot="reference" type="text" >删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->

      </div>
    </div>
    <ImportExcel
      @updateEmployee="loadEmployment" 
      :isShowExcel.sync="isShowExcel"
      />

    <AssignRole 
      :isShowRole.sync="isShowRole"
      :curId="curId"
      ref="refRole"
      />
  </div>
</template>

<script>
import { arrTree } from '@/utils'
import { getdepartment } from '@/api/department'
import { getEmployeeList, getExcelList, delUserExcel } from '@/api/employee'
import FileSaver  from 'file-saver'
import ImportExcel from './components/import-excel.vue'
import AssignRole from './components/assignRole.vue'

export default {
  components: { ImportExcel, AssignRole },
  name: 'Employee',
  data() {
    return {
      isShowExcel: false,
      isShowRole: false,
      curId: -1,
      queryObj: {
        page: 1,
        pagesize: 10,
        keyword: null,
        departmentId: null
      },
      total: 0,
      employList: [],
      depts: [{
        name: '传智教育',
        children: [
          { name: '总裁办' },
          { name: '行政部' },
          { name: '财务部' }
        ]
      }],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    assignShow(id) {
      this.curId = id
      this.isShowRole = true
      this.$nextTick(() =>{
        this.$refs.refRole.getRoleCur()
      })
    },
    async delUser(id){
      await delUserExcel(id)
      this.$message.success('删除成功')
      this.getDepartment()
    },
    async hExcel() {
      const res = await getExcelList()
      console.log(res)
      FileSaver.saveAs(res, '员工信息.xls')
    },
    async getDepartment() {
      const res = await getdepartment()
      this.depts = arrTree(res.data, 0)
      // console.log(res)
      // console.log(this.depts)
      const id = this.depts[0].id
      // console.log(id)
      this.queryObj.departmentId = id
      this.$nextTick(() => this.$refs.tree.setCurrentKey(id))
      this.loadEmployment()
    },
    async loadEmployment() {
      // console.log('查询条件是', this.queryObj)
      // axios在发请求的时候，如果你的参数值为null，它会丢弃这个参数
      const res = await getEmployeeList(this.queryObj)
      console.log(res)
      this.total = +res.data.total
      this.employList = res.data.rows
    },
    hCurrentChange(data, node) {
      // console.log(data, node)
      // 记录id
      this.queryObj.departmentId = data.id
      // 发请求拿到对应的部门的信息
      this.loadEmployment()
    },
    handleCurrent(page) {
      this.queryObj.page = page
      this.getDepartment()
    },
    hInput() {
      clearTimeout(this.timeId)
      this.timeID = setTimeout(() => {
        this.queryObj.page = 1
        this.loadEmployment()
      }, 500)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;

    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
    img{
      text-align: center;
    }
  }
  .buttonsss{
    margin: 0 10px;
  }
}

</style>

